package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ASM_FdebtorDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;
	
	
	public ASM_FdebtorDS(Context context) {
		this.context = context;
		dbHelper  = new ASM_DB(context);
		
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	public int Create(ASM_Fdebtor det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		 
	
		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmFdebtor_DebCode, det.getAsmFdebtor_DebCode());
		values.put(ASM_DB.asmFdebtor_DebName, det.getAsmFdebtor_DebName() );
		values.put(ASM_DB.asmFdebtor_AreaCode, det.getAsmFdebtor_AreaCode());
		values.put(ASM_DB.asmFdebtor_DealCode, det.getAsmFdebtor_DealCode());
		values.put(ASM_DB.asmFdebtor_RepCode, det.getAsmFdebtor_RepCode());
		values.put(ASM_DB.asmFdebtor_OutCode, det.getAsmFdebtor_OutCode());
		values.put(ASM_DB.asmFdebtor_AreaName, det.getAsmFdebtor_AreaName());
		
		count = (int) dB.insert(ASM_DB.ASM_FDEBTOR, null, values);

		dB.close();
		return count;
		
	}
	public ArrayList<String> getDebtors(String AreaCODE) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		
		
		String selectQuery = "select DebCode,DebName from fDebtor where AreaCode in(select AreaCode From fArea where AreaCode = '"+AreaCODE+"')";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String namecode = cursor.getString(1)+" -:- "+cursor.getString(0);
			Details.add(namecode);
			
			
			
		}
		
		return Details;
	}
	
	public ArrayList<String> getselectDeb(String Routecode) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select a.DebCode,a.DebName from fDebtor as a inner join fRouteDet as b on a.DebCode = b.DebCode where RouteCode = '"+Routecode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String namecode = cursor.getString(1)+" -:- "+cursor.getString(0);
			Details.add(namecode);
			
			
			
		}
		
		return Details;
	}
}
